package org.gesplan.controller;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.swing.JComboBox;

import org.gesplan.util.Utils;

import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.matchers.TextMatcherEditor;
import ca.odell.glazedlists.swing.AutoCompleteSupport;


/**
 *
 * @author David
 */
public class GenericController {

    public static EntityManager getEntityManager() {
        return Utils.getEntityManagerFactory().createEntityManager();        
    }
  
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static AutoCompleteSupport fillComboBox(String sql, JComboBox comboBox) {
        
        AutoCompleteSupport support = null;
        try{
            Query query = getEntityManager().createQuery(sql); 
            List<Object> objectList = query.getResultList();        
            EventList<Object> eventList = GlazedLists.eventList(objectList);   
            support = AutoCompleteSupport.install(comboBox, eventList);
            support.setFilterMode(TextMatcherEditor.CONTAINS);
        } catch (Exception e){
            Utils.getLogger().severe(e.getMessage());
        }            
        return support;
        
    }    
    
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static AutoCompleteSupport fillComboBox(String sql, JComboBox comboBox, AutoCompleteSupport support) {
        
        try {
            Query query = getEntityManager().createQuery(sql); 
            List<Object> objectList = query.getResultList();        
            EventList<Object> eventList = GlazedLists.eventList(objectList);   
            if (support != null && support.isInstalled()){
                support.uninstall();
            }
            support = AutoCompleteSupport.install(comboBox, eventList);
            support.setFilterMode(TextMatcherEditor.CONTAINS);
        } catch (Exception e){
            Utils.getLogger().severe(e.getMessage());
        }
        return support;
        
    }    
    
    
    public static void test(String sql){
        try {
            Query query = getEntityManager().createQuery(sql); 
            query.getResultList();        
        } catch (Exception e){
            Utils.getLogger().severe(e.getMessage());
        }        
    }
    
    
    public static Double getMaterialPvp(String id){
        
        String sql = "SELECT pvp FROM Material WHERE id = '" + id + "'";
        PreparedStatement ps;
        try {
            Double pvp;            
            ps = Utils.getConnection().prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                pvp = rs.getDouble("pvp");
                return pvp;
            }            
        } catch (SQLException e) {
            Utils.getLogger().severe(e.getMessage());
        }
        return -1.0;
        
    }
    
            
}